Page({
    data: {
        // 标签页状态 0: 缴费页面, 1: 缴费记录
        currentTab: 0,

        // 缴费项目数据
        paymentItems: [
            {
                id: 1,
                title: '学费',
                type: '必缴费用',
                amount: 5800.00,
                description: '2024年春季学期学费',
                deadline: '2024-03-15',
                status: 'unpaid',
                department: '财务处'
            },
            {
                id: 2,
                title: '住宿费',
                type: '必缴费用',
                amount: 1200.00,
                description: '2024年春季学期住宿费',
                deadline: '2024-03-15',
                status: 'unpaid',
                department: '宿舍管理中心'
            },
            {
                id: 3,
                title: '教材费',
                type: '代收费用',
                amount: 380.00,
                description: '2024年春季学期教材费用',
                deadline: '2024-03-10',
                status: 'paid',
                department: '教务处'
            },
            {
                id: 4,
                title: '实验费',
                type: '选缴费用',
                amount: 150.00,
                description: '计算机实验室使用费',
                deadline: '2024-04-01',
                status: 'unpaid',
                department: '实验中心'
            }
        ],

        // 缴费记录数据
        paymentRecords: [
            {
                id: 1,
                title: '教材费',
                amount: 380.00,
                paymentDate: '2024-01-15',
                status: 'success',
                orderNumber: 'PAY202401150001',
                paymentMethod: '微信支付',
                department: '教务处'
            },
            {
                id: 2,
                title: '实验费',
                amount: 150.00,
                paymentDate: '2024-01-10',
                status: 'success',
                orderNumber: 'PAY202401100001',
                paymentMethod: '支付宝',
                department: '实验中心'
            }
        ],

        // 缴费成功弹窗相关数据
        showSuccessModal: false,
        currentPaymentItem: {},
        currentPaymentTime: '',
        currentOrderNumber: ''
    },

    onLoad(options) {
        this.loadPaymentRecords();

        // 如果传入了 tab 参数，切换到对应的标签页
        if (options.tab) {
            this.setData({
                currentTab: parseInt(options.tab)
            });
        }
    },

    /**
     * 切换标签页
     */
    switchTab(e) {
        const tab = e.currentTarget.dataset.tab;
        this.setData({
            currentTab: parseInt(tab)
        });
    },

    /**
     * 加载支付记录
     */
    loadPaymentRecords() {
        // 这里应该从服务器获取真实数据
        // 目前使用模拟数据
        wx.showLoading({
            title: '加载中...'
        });

        setTimeout(() => {
            wx.hideLoading();
            this.setData({
                paymentRecords: this.data.paymentRecords
            });
        }, 1000);
    },

    /**
     * 查看支付详情
     */
    viewPaymentDetail(e) {
        const record = e.currentTarget.dataset.record;
        wx.showModal({
            title: '支付详情',
            content: `项目：${record.title}\n金额：￥${record.amount}\n支付时间：${record.paymentDate}\n订单号：${record.orderNumber}\n支付方式：${record.paymentMethod}\n收费方：${record.department}`,
            showCancel: false
        });
    },

    /**
     * 单项缴费
     */
    payItem(e) {
        const item = e.currentTarget.dataset.item;

        if (item.status === 'paid') {
            wx.showToast({
                title: '该项目已缴费',
                icon: 'none'
            });
            return;
        }

        // 显示支付确认弹窗
        wx.showModal({
            title: '确认缴费',
            content: `项目：${item.title}\n金额：￥${item.amount.toFixed(2)}\n\n确认支付吗？`,
            success: (res) => {
                if (res.confirm) {
                    this.processPayment(item);
                }
            }
        });
    },

    /**
     * 处理支付
     */
    processPayment(item) {
        wx.showLoading({
            title: '支付中...'
        });

        // 模拟支付过程
        setTimeout(() => {
            wx.hideLoading();

            // 生成订单号
            const orderNumber = 'PAY' + new Date().getTime();
            const paymentTime = new Date().toLocaleString('zh-CN');

            // 更新支付项目状态
            const paymentItems = [...this.data.paymentItems];
            const itemIndex = paymentItems.findIndex(p => p.id === item.id);
            paymentItems[itemIndex].status = 'paid';

            // 添加到支付记录
            const newRecord = {
                id: this.data.paymentRecords.length + 1,
                title: item.title,
                amount: item.amount,
                paymentDate: paymentTime,
                status: 'success',
                orderNumber: orderNumber,
                paymentMethod: '微信支付',
                department: item.department
            };

            const paymentRecords = [newRecord, ...this.data.paymentRecords];

            // 更新数据并显示成功弹窗
            this.setData({
                paymentItems,
                paymentRecords,
                showSuccessModal: true,
                currentPaymentItem: item,
                currentPaymentTime: paymentTime,
                currentOrderNumber: orderNumber
            });

        }, 2000);
    },

    /**
     * 关闭缴费成功弹窗
     */
    closeSuccessModal() {
        this.setData({
            showSuccessModal: false,
            currentPaymentItem: {},
            currentPaymentTime: '',
            currentOrderNumber: ''
        });
    },

    /**
     * 查看缴费详情
     */
    viewDetails(e) {
        const item = e.currentTarget.dataset.item;
        wx.showModal({
            title: item.title,
            content: `类型：${item.type}\n金额：￥${item.amount}\n描述：${item.description}\n截止日期：${item.deadline}\n收费方：${item.department}`,
            showCancel: false
        });
    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {
        this.loadPaymentRecords();
        wx.stopPullDownRefresh();
    }
}); 